/*
 * @Author: your name
 * @Date: 2020-07-08 09:59:49
 * @LastEditTime: 2021-04-17 18:13:33
 * @LastEditors: Cao Sx
 * @Description: In User Settings Edit
 * @FilePath: /zyou_b_end/lib/event/event_bus.dart
 */

import 'package:flutter/material.dart';

///订阅者回调签名
typedef EventCallback = void Function(dynamic);

class EventBus {
  /// 私有构造函数
  EventBus._instance();

  static final EventBus _singleton = EventBus._instance();

  ///工厂构造函数
  factory EventBus() => _singleton;

  /// 保存事件订阅者队列，key:事件名(id)，value: 对应事件的订阅者队列
  final _eventMap = <Object, List<EventCallback>>{};

  /// 添加订阅者
  void on(eventName, EventCallback f) {
    if (eventName == null) return;
    _eventMap[eventName] = <EventCallback>[];
    _eventMap[eventName].add(f);
  }

  /// 移除订阅者
  void off(eventName, [EventCallback f]) {
    var list = _eventMap[eventName];
    if (eventName == null || list == null) return;
    if (f == null) {
      _eventMap[eventName] = null;
    } else {
      list.remove(f);
    }
  }

  /// 触发事件，事件触发后该事件所有订阅者会被调用
  void emit(eventName, [arg]) {
    var list = _eventMap[eventName];
    if (list == null) return;
    var len = list.length - 1;
    //反向遍历，防止订阅者在回调中移除自身带来的下标错位
    for (var i = len; i > -1; --i) {
      list[i](arg);
    }
  }

  Future showRemindDialog(BuildContext context, Function() callback) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
              title: Text("删除区域防护"),
              content: Text("确定删除当前区域防护设置？"),
              actions: <Widget>[
                ElevatedButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("取消")),
                    style: ButtonStyle(
                      shape: MaterialStateProperty.all(StadiumBorder(
                          side: BorderSide(
                              //设置 界面效果 圆角弧度
                              style: BorderStyle.solid,
                              color: Colors.grey[400]))),
                      foregroundColor:
                          MaterialStateProperty.all(Color(0xff333333)),
                      backgroundColor: MaterialStateProperty.all(Colors.white),
                      textStyle:
                          MaterialStateProperty.all(TextStyle(fontSize: 17)),
                    )),
                ElevatedButton(
                    onPressed: () async {
                      var callback2 = callback;
                      return callback2;
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("确定")),
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.orange),
                        textStyle:
                            MaterialStateProperty.all(TextStyle(fontSize: 17)),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果 圆角弧度
                          style: BorderStyle.solid,
                          color: Color(0xffFF7F24),
                        )))))
              ]);
        });
    return null;
  }
}
